<?php
//namespace student\controller\welcome;
defined('BASEPATH') OR exit('No direct script access allowed');

class Getorder extends CI_Model {


	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
	}

    /**
	*  插入订单数据
    */
	public function insertData($array)
	{
		$order_id = $array['order_id'];
		$order_name = $array['order_name'];
		$unit_price = $array['unit_price'];
		$order_num = $array['order_num'];
		$total_price = $array['total_price'];
		$discount = $array['discount'];
		$owner_id = $array['owner_id'];
		$buyer_id = $array['buyer_id'];
		$buy_time = $array['buy_time'];
		$last_time = $array['last_time'];
		$pay_type = $array['pay_type'];

		$sql = "insert into order_info (order_id,order_name,unit_price,order_num,total_price,discount,owner_id,buyer_id,buy_time,last_time,pay_type) values ($order_id,'$order_name',$unit_price,$order_num,$total_price,'$discount',$owner_id,$buyer_id,'$buy_time','$last_time',$pay_type)";
		$res = $this->db->query($sql);
		if($res){
			return true;
		} else {
			return false;
		}
	}


	/**
	** 生成订单号
	**/
	public function setOrderId($dataId)
	{
		$buyer = @$_SESSION['user_id']?@$_SESSION['user_id']:1;
		$sql = "select user_id from data_info where id = ?";
		$res = $this->db->query($sql,$dataId)->row_array();
		$owner = $res['user_id'];
		$res = array('orderId'=>''.$buyer.date("YmdHis").$owner,'owner_id'=>$owner);
		return $res;
	}


	/**
	** 通过user_id 查询个人所有订单
	**/
	public function getOrderinfo($user_id,$status=5,$content='')
	{
		if($status == 1){
			if(empty($content)){
				$sql = "select * from order_info where buyer_id = ? and status = 1";
			} else {
				$sql = "select * from order_info where buyer_id = ? and status = 1 and order_name like '%$content%'";
			}	
		} else if($status == 2){
			if(empty($content)){
				$sql = "select * from order_info where buyer_id = ? and status = 2";
			} else {
				$sql = "select * from order_info where buyer_id = ? and status = 2 and order_name like '%$content%'";
			}
		} else {
			if(empty($content)){
				$sql = "select * from order_info where buyer_id = ?";
			} else {
				$sql = "select * from order_info where buyer_id = ? and order_name like '%$content%'";
			}
			
		}
		$res = $this->db->query($sql,$user_id)->result_array();
		return $res;
	}

	/**
	* 通过订单号 查询订单信息
	**/
	public function getOrderByorderid($order_id)
	{
		$sql = "select * from order_info where order_id = ?";
		$res = $this->db->query($sql,$order_id)->row_array();
		return $res;
	}

	/**
	** 通过订单号，删除订单
	**/
	public function delOrder($orderId)
	{
		$sql = "update order_info set status = 4 where order_id = ?";
		$res = $this->db->query($sql,$orderId);
		return $res;
	}


    /**
    ** 通过订单号，进行付款
    **/
    public function goPay($orderId,$type)
    {
    	switch($type){
    		case "alipay": $type = 1;break;
    		case "weixinpay":$type = 2;break;
    		case "accountpay":$type = 3;break;
    		default:$type = 4;break;
    	}
    	$sql = "update order_info set pay_type=?,status = 2,last_time = ?,pay_time=? where order_id = ?";
    	$data = array($type,date("Y-m-d H:i:s"),date("Y-m-d H:i:s"),$orderId);
    	$res = $this->db->query($sql,$data);
    	return $res;
    }
}
